﻿using maxiaopeng.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace maxiaopeng.Controllers
{
    public class ModulesController : ApiController
    {
        // GET: api/Modules
        [HttpGet]
        public IHttpActionResult getmoudulelist()
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var AuthorizeInfo = db.Modules.Where(p => true);
                var tempdata = from a in AuthorizeInfo
                               where a.ParentId == 0
                               select new
                               {
                                   Id = a.Id,
                                   Name = a.Name,
                                   ParentId = a.ParentId,
                                   Path = a.Path,
                                   Weight = a.Weight,
                                   children = from t in AuthorizeInfo
                                              where t.ParentId == a.Id
                                              select new
                                              {
                                                  Id = t.Id,
                                                  Name = t.Name,
                                                  ParentId = t.ParentId,
                                                  Path = t.Path,
                                                  Weight = t.Weight
                                              }
                               };
                return Json(new { data = tempdata.ToList() });
            }
        }
        public IHttpActionResult editmoudules([FromBody] Modules module)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var themoudu = db.Modules.FirstOrDefault(x => x.Id == module.Id);
                themoudu.Name = module.Name;
                themoudu.Path = module.Path;
                themoudu.Weight = module.Weight;
                themoudu.ParentId = module.ParentId;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "保存成功" });
                }
                else
                {
                    return Json(new { code = 0, msg = "保存失败" });
                }
            }
        } 
        //删除模块
        public IHttpActionResult delmoudules([FromBody] Modules mou)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                //查找到删除的模块
                var themoudule = db.Modules.FirstOrDefault(x => x.Id == mou.Id);
                //找到使用该模块的角色
                var rolemoudul = db.Rolemodules.Where(x => x.RoleId == themoudule.Id).ToList();
                //删除使用该模块的角色
                if (rolemoudul != null)
                {
                    foreach (var item in rolemoudul)
                    {
                        db.Rolemodules.Remove(item);
                    }
                    db.SaveChanges();
                    if (themoudule.ParentId == 0)
                    {
                        var parentlist = db.Modules.Where(x => x.ParentId == themoudule.Id).ToList();
                        if (parentlist != null)
                        {
                            foreach (var item in parentlist)
                            {
                                db.Modules.Remove(item);
                            }
                            db.SaveChanges();
                            db.Modules.Remove(themoudule);
                            db.SaveChanges();
                            int a = db.SaveChanges();
                            if (a > 0)
                            {
                                return Json(new { code = 0, msg = "删除成功" });                            
                            }
                            else
                            {
                                return Json(new { code = -1, msg = "删除失败" });
                            }
                        }
                        else
                        {
                            db.Modules.Remove(themoudule);
                            int a = db.SaveChanges();
                            if (a > 0)
                            {
                                return Json(new { code = 0, msg = "删除成功" });
                            }
                            else
                            {
                                return Json(new { code = -1, msg = "删除失败" });
                            }
                        }
                    }
                    db.Modules.Remove(themoudule);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功" });                    
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败" });
                    }
                }
                else
                {
                    if (themoudule.ParentId == 0)                    
                    {
                        var parentlist = db.Modules.Where(x => x.ParentId == themoudule.Id).ToList();
                        if (parentlist != null)
                        
                        {
                            foreach (var item in parentlist)

                            {
                                db.Modules.Remove(item);
                            }
                            db.SaveChanges();
                            db.Modules.Remove(themoudule);                        
                            int a = db.SaveChanges();
                            if (a > 0)
                            {
                                return Json(new { code = 0, msg = "删除成功" });                            
                            }
                            else
                            {                            
                                return Json(new { code = -1, msg = "删除失败" });
                            }
                        }
                        else
                        {
                            db.Modules.Remove(themoudule);
                            db.SaveChanges();
                            int a = db.SaveChanges();
                            if (a > 0)
                            {
                                return Json(new { code = 0, msg = "删除成功" });
                            }
                            else
                            {
                                return Json(new { code = -1, msg = "删除失败" });
                            }                        
                        }
                    }                
                    db.Modules.Remove(themoudule);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败" });
                    }
                }
            }
        }
        //新增模块
        public IHttpActionResult addmoudules([FromBody] Modules module)
        {

            using (CRMDBEntities db = new CRMDBEntities())
            {
                var themoudu = db.Modules.FirstOrDefault(x => x.Name == module.Name);
                if (themoudu != null)
                {
                    return Json(new { code = -1, msg = "已存在" });
                }
                else
                {
                    db.Modules.Add(module);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "添加成功" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "添加失败" });
                    }
                }
            }
        }

    }
}
